﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GamePlugin.Logics;
using GamePlugin.Logics.Core;
using System.Windows.Forms;
using System.IO;
using GamePlugin.IO;
using GamePlugin.Logics.Data;

namespace GamePlugin.Controls
{
    /// <summary>
    /// Interaction logic for SearchInfoList.xaml
    /// </summary>
    public partial class SearchInfoList : System.Windows.Controls.UserControl
    {
        public SearchInfoList()
        {
            InitializeComponent();

            searchListBox.DataContext = Workspace.Instance.SearchInfoItemList;
            if (Workspace.Instance.SearchInfoItemList.Children.Count > 0)
            {
                searchListBox.SelectedIndex = 0;
                Helper.GetMainWindow().searchInformation.DataContext = Workspace.Instance.SearchInfoItemList.Children[0];
            }
        }

        private void searchListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Helper.GetMainWindow().searchInformation.DataContext = e.AddedItems[0];

                Helper.GetMainWindow().searchInformation.UpdateComboxAndPropertyItem();

                string text = (e.AddedItems[0] as SearchItem).SearchInfoName;
                text = text.Substring(text.IndexOf("-") + 1);
                textBox1.Text = text;
            }
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            SearchInfoItemList list = (SearchInfoItemList)(searchListBox.DataContext);
            SearchItem one = new SearchItem(textBox1.Text);
            one.CopyFrom(Helper.GetMainWindow().searchInformation.DataContext as SearchItem);
            Helper.GetMainWindow().searchInformation.DataContext = one;
            list.Children.Add(one);
            Helper.GetMainWindow().searchInfoList.searchListBox.SelectedIndex = list.Children.Count - 1;
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            int index = searchListBox.SelectedIndex;
            if (index >= 0)
            {
                SearchInfoItemList list = (SearchInfoItemList)(searchListBox.DataContext);
                list.Children.RemoveAt(index);

                if (index > 0) index--; else index = 0;
                if (list.Children.Count > 0)
                    Helper.GetMainWindow().searchInformation.DataContext = list.Children[index];
                else
                    Helper.GetMainWindow().searchInformation.DataContext = new SearchItem();
            }
        }

        private void ModifyData_Click(object sender, RoutedEventArgs e)
        {
            int index = searchListBox.SelectedIndex;
            if (index >= 0)
            {
                SearchInfoItemList list = (SearchInfoItemList)(searchListBox.DataContext);
                list.Children[index].SearchInfoName = textBox1.Text;
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = IOManager.Instance.DataFolderPath;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.FileName = "DefaultList.pak";
            saveFileDialog1.Filter = "Package|*.pak";
            saveFileDialog1.Title = "Save search list";
            ;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (saveFileDialog1.FileName == null || saveFileDialog1.FileName.Length == 0)
                    return;
                IOManager.Instance.SaveSearchData(Workspace.Instance.SearchOptionData, searchListBox.DataContext as SearchInfoItemList, saveFileDialog1.FileName);
            }
        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = IOManager.Instance.DataFolderPath;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FileName = "DefaultList.pak";
            openFileDialog.Filter = "Package|*.pak";
            openFileDialog.Title = "Load search list";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog.FileName == null || openFileDialog.FileName.Length == 0)
                    return;

                SearchInfoItemList list = searchListBox.DataContext as SearchInfoItemList;
                IOManager.Instance.LoadSearchData(Workspace.Instance.SearchOptionData, list, openFileDialog.FileName);

                if (list.Children.Count > 0)
                {
                    searchListBox.SelectedIndex = 0;
                }
            }
        }

        private void FreshButton_Click(object sender, RoutedEventArgs e)
        {
            if (Helper.GetMainWindow() != null && Helper.GetMainWindow().searchInformation != null)
                Helper.GetMainWindow().searchInformation.UpdateComboxAndPropertyItem();
        }

        private void MoveUpButton_Click(object sender, RoutedEventArgs e)
        {
            Workspace.Instance.SearchInfoItemList.SelectItemUp();
        }

        private void MoveDownButton_Click(object sender, RoutedEventArgs e)
        {
            Workspace.Instance.SearchInfoItemList.SelectItemDown();
        }

        private void AutoSortButton_Click(object sender, RoutedEventArgs e)
        {
            Workspace.Instance.SearchInfoItemList.Sort();
        }
    }
}
